import {request} from "../../request/index"
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: '综合',
        isActive: true
      },
      {
        id: 1,
        value: '销量',
        isActive: false
      },
      {
        id: 2,
        value: '价格',
        isActive: false
      },
    ]
  },
  goodsList: [],
  //接口要的数据
  QueryParams: {
    query:"",
    cid: "",
    pagenum: 1,
    pagesize: 10
  },
  totalPages: 1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.QueryParams.cid = options.cid || ''
    this.QueryParams.query = options.query || ''
    this.getDoodsList()
  },

  async getDoodsList() {
    let res = await request({url:'/goods/search', data:this.QueryParams})
    //获取总条数
    let total = res.total
    this.totalPages = Math.ceil(total / this.QueryParams.pagesize)

    this.setData({
      //拼接数组
      goodsList: this.data.goodsList?this.data.goodsList.concat(res.goods):res.goods
 
    })
    //关闭下拉刷新
    wx.stopPullDownRefresh()
  },

  //标题点击事件  从子组件传递过来
  handleTabItemChange(e) {
    // console.log(e);
    //获取被点击的标题索引
    let {index} = e.detail;
    //修改原数组
    // console.log(this.data);
    let {tabs} = this.data
    tabs.forEach((v,i) => i==index?v.isActive=true:v.isActive=false)
    this.setData({
      tabs
    })
  },

  //页面上滑滚动条触底事件
  onReachBottom() {
    //判断有没有下一页
    if(this.QueryParams.pagenum >= this.totalPages) {
      wx.showToast({title:"没有下一页数据了"})
    } else {
      this.QueryParams.pagenum++
      this.getDoodsList()
    }
  },

  //监听下拉刷新
  onPullDownRefresh() {
    //重置
    this.setData({
      goodsList: []
    })
    this.QueryParams.pagenum = 1
    this.getDoodsList()
  }
})